<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/jquery.min.js"></script>
    <script src="/bootstrap.js"></script>
    <link href="/bootstrap.css" rel="stylesheet">
    <script src="/axios.min.js"></script>

</head>
<body>
<div class="container">
    <h1>The Activity Page</h1>
    <button type="button" class="btn btn-primary btn-sm"
            data-toggle="modal" data-target="#myModal">创建活动
    </button>
    <!--模态框-->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog"
         aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">创建活动</h4>
                </div>
                <div class="modal-body">





                    <form class="form-horizontal"  method="post" id="saveFormId">
                        <div class="form-group">
                            <label for="titleId" class="col-sm-2 control-label">标题</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control"  name="title" id="titleId" placeholder="title">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="categoryId" class="col-sm-2 control-label">类型</label>
                            <div class="col-sm-10">
                                <select id="categoryId" name="category" class="form-control">
                                    <option value="教育培训">教育培训</option>
                                    <option value="企业活动">企业活动</option>
                                    <option value="交友活动">交友活动</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group" >
                            <label for="startTimeId" class="col-sm-2 control-label">开始时间</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control form_datetime"
                                       name="startTime" id="startTimeId" placeholder="start time">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="endTimeId" class="col-sm-2 control-label">结束时间</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control form_datetime"
                                       name="endTime" id="endTimeId" placeholder="end time">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="remarkId" class="col-sm-2 control-label">备注</label>
                            <div class="col-sm-10">
           <textarea type="text" class="form-control"  rows="5" name="remark" id="remarkId">
            </textarea>
                            </div>
                        </div>
                    </form>
                </div>





                <div class="modal-footer">
                    <button type="button" class="btn btn-default"data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" onclick="doSaveObject()">
                        Save Changes</button>
                </div>
            </div>
        </div>
    </div>
    <table class="table">
        <thead>
        <tr>
            <th>title</th>
            <th>Category</th>
            <th>StartTime</th>
            <th>EndTime</th>
            <th>State</th>
            <th colspan="2">option</th>
        </tr>
        </thead>
        <tbody id="tbodyId">
        <tr><td colspan="6">数据正在加载中.......</td></tr>
        </tbody>
    </table>

</div>
<script>
    function deDeleteById(id) {
        axios.get("http://localhost:8081/activity/deleteById/" + id).then(
            function(resp){
                alert(resp.data);
                doFindAll();
         })
    }

    function doUpdateById1(item){
        $.post("http://localhost:8081/activity/updateById",JSON.stringify({"id":111,"title":item.title,"category":item.category,"startTime":item.startTime,"endTime":item.endTime}),function(resp){
                alert(resp.data);
                doFindAll();
        })

    }

    // function doUpdateById() {
    //     debugger;
    //     $.post("http://localhost:8081/activity/updateById",JSON.stringify({"id":id,"title":title,"category":category,"startTime":startTime,"endTime":endTime,"state":state}),(resp) => {
    //         alert(resp.data);
    //         doFindAll();
    //
    //     })
    // }

    function doUpdateById(id){
        $.get("http://localhost:8081/activity/selectOne/"+id,function (resp) {
            alert(resp.data.title);
            $("#titleId").val(resp.data.title);
            $("#startTimeId").val(resp.data.startTime);
            $("#endTimeId").val(resp.data.endTime);
            $("#remarkId").val(resp.data.remark);
            console.log(resp.data);
        })
    }


    function doSaveObject() {
        let url = "/activity/insertActivity";
        let params = $("#saveFormId").serialize();//jquery中获取表单数据的一种方式
        console.log("params", params);
        $.post(url, params, (result) => {
            alert(result);
            $('#myModal').modal('hide');
            doFindAll();
        })
    }


    function doFindAll(){
        let url="/activity/findAll";
        $.getJSON(url,function(result){//[{},{},{},...]
            let tBody=$("#tbodyId");
            tBody.empty();
            result.forEach((item)=>{
                let tr=`<tr>
                           <td>${item.title}</td>
                           <td>${item.category}</td>
                           <td>${item.startTime}</td>
                           <td>${item.endTime}</td>
                           <td>${item.state?"有效":"无效"}</td>
                           <td><button onclick="deDeleteById(${item.id})" type="button" class="btn btn-danger btn-sm">删除</button></td>
                           <td><button onclick="doUpdateById(${item.id})" data-toggle="modal" data-target="#myModal" type="button" class="btn btn-danger btn-sm">修改</button></td>
                        </tr>`
                tBody.append(tr);
            });
        });
    }
    doFindAll();
</script>
</body>
</html>